﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BotonJXC.Entity;
using NHibernate;
using NHibernate.Criterion;
using BotonJXC.Util;

namespace BotonJXC.DAO
{
    public class AcceptanceDao : NHibernateDao<Acceptance, int>
    {

        public void SaveAcceptance(Acceptance Acceptance)
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {

                base.Save(Acceptance);
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
                throw;

            }
            finally
            {
                HBHelper.Close();
            }

        }
        public void Modify(Acceptance Acceptance)
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {

                base.Update(Acceptance);
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
                throw;

            }
            finally
            {
                HBHelper.Close();
            }

        }

        public PageModel<Acceptance> FindPage(Acceptance Acceptance, DateTime? begintime, DateTime? endtime, int start, int count)
        {
            List<ICriterion> criterions = new List<ICriterion>();

            if (begintime != null)
            {
                criterions.Add(Restrictions.Ge("CreateTime", begintime));
            }
            if (endtime != null)
            {
                criterions.Add(Restrictions.Le("CreateTime", endtime));
            }
            if (Acceptance.OrgId > 0)
            {
                criterions.Add(Restrictions.Eq("OrgId", Acceptance.OrgId));
            }
            if (Acceptance.OrgDepId > 0)
            {
                criterions.Add(Restrictions.Eq("OrgId", Acceptance.OrgDepId));
            }
            if (Utils.IsNotBlank(Acceptance.AcceptanceType))
            {
                criterions.Add(Restrictions.Eq("AcceptanceType", Acceptance.AcceptanceType));
            }
            //if (Utils.IsNotBlank(Acceptance.InSn))
            //{
            //    criterions.Add(Restrictions.Eq("OrgId", Acceptance.InSn));
            //}
            //if (Utils.IsNotBlank(Acceptance.AuditStatus))
            //{
            //    criterions.Add(Restrictions.Eq("OrgId", InStocks.AuditStatus));
            //}
            //if (Acceptance.Warehouse != null)
            //{
            //    criterions.Add(Restrictions.Eq("Warehouse.WarehouseId", InStocks.Warehouse.WarehouseId));
            //}
            Order order = new Order("CreateTime", false);
            return base.FindByPage(criterions.ToArray(), order, start, count);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="orderSn"></param>
        /// <returns></returns>
        public IList<Acceptance> findOutStocksBySn(string AcceptanceSn, string AcceptanceType)
        {
            List<ICriterion> criterions = new List<ICriterion>();
            if (Utils.IsNotBlank(AcceptanceSn))
            {
                criterions.Add(Restrictions.Eq("AcceptanceSn", AcceptanceSn));
            }
            if (Utils.IsNotBlank(AcceptanceType))
            {
                criterions.Add(Restrictions.Eq("AcceptanceType", AcceptanceType));
            }
            return base.Find(criterions.ToArray());
        }
    }
}
